package com.example.services;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.os.Message;

import com.example.entities.MP3;
import com.example.musicplayer01.MainActivity;

import java.io.IOException;
import java.util.List;

public class MusicService extends Service implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
    public static MediaPlayer mediaPlayer=null;
    private Uri uri;
    private MusicProgress musicProgress;
    public static boolean musicSwitch=false;
    public static int index=0;
    public static int progress=0;
    public static List<MP3> data=null;
    public static String action="";//相关操作,静态变量记得设置缺省值

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer=new MediaPlayer();
        uri=Uri.parse(data.get(index).getUrl());//获取数据路径
        try{
            mediaPlayer.setDataSource(this,uri);//获取播放数据
        }catch (Exception e){
            e.printStackTrace();
        }
        mediaPlayer.setOnPreparedListener(this);//准备数据时调用
        mediaPlayer.setOnCompletionListener(this);//完成时调用
        mediaPlayer.setOnErrorListener(this);//报错时调用
        mediaPlayer.prepareAsync();//异步准备数据

        //启动progress线程
        musicProgress=new MusicProgress();
        musicProgress.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(action.equals("changeMusic")) nextMusic();
        if(action.equals("playOrPause")) changeMusicStatus();
        return super.onStartCommand(intent, flags, startId);
    }

    private void changeMusicStatus(){
        if(mediaPlayer!=null){
            if(musicSwitch){
                mediaPlayer.start();
            }else{
                mediaPlayer.pause();
            }
        }
        action="";
    }

    @Override
    public void onDestroy() {
        musicSwitch=false;
        musicProgress=null;
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer=null;
        super.onDestroy();
    }

    private void nextMusic(){
        musicSwitch=false;
        mediaPlayer.stop();
        mediaPlayer.reset();//重置初始化
        uri=Uri.parse(data.get(index).getUrl());//获取数据路径
        try{
            mediaPlayer.setDataSource(this,uri);//获取播放数据
            mediaPlayer.prepareAsync();//异步准备数据
        }catch (Exception e){
            e.printStackTrace();
        }
        action="";//结束操作识别
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        index++;
        Message msg=new Message();
        indexJudge();
        msg.what=200;
        MainActivity.handler.sendMessage(msg);
    }

    private void indexJudge(){
        if(index>=data.size()) index=0;
        else if(index<0) index=data.size()-1;
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        startMusic();
    }

    private void startMusic(){
        mediaPlayer.start();
        musicSwitch=true;
    }

    class MusicProgress extends Thread{
        @Override
        public void run() {
            while(true){
                if(mediaPlayer!=null){
                    if(musicSwitch && mediaPlayer.isPlaying()){
                        Message msg=new Message();
                        msg.what=100;
                        progress=mediaPlayer.getCurrentPosition();
                        msg.arg1=progress;
                        MainActivity.handler.sendMessage(msg);
                    }
                }
            }
        }
    }
}